package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.RedisConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.dto.SetmealXiuGaiDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealHuiXianVO;
import com.sky.vo.SetmealPageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

@Service

public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 新增套餐
     * */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addSetmeal(SetmealDTO setmealDTO) {
        //TOOD套餐名称唯一
        String name = setmealDTO.getName();
        System.out.println(name+"------------------------------");
        Setmeal setmeal1= setmealMapper.seleteByName(name);
        if(setmeal1!=null){
            throw new BaseException("用户名不能重复");
        }



        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //加setmeal表
        setmealMapper.addSetmeal(setmeal);
        //加setmeal_dish表
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(!CollectionUtils.isEmpty(setmealDishes)) {
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmeal.getId());
            }
            setmealMapper.addSetmealDish(setmealDishes);
        }

    }
    /**
     * 套餐分页查询
     * */

    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealPageVO>dtos= setmealMapper.page(setmealPageQueryDTO);
        return new PageResult(dtos.getTotal(),dtos.getResult());
    }
    /**
     * 删除套餐
     * */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(List<Long> ids) {
        //其手中不能删
        for(Long id:ids){
            Setmeal setmeal=setmealMapper.seleteById(id);
            //查询返回带id
//            if(setmeal==null){
//                throw new BaseException("sssss");
//            }
            if(Objects.equals(setmeal.getStatus(), StatusConstant.ENABLE)){
                throw new BaseException("起售中的套餐不能出售");
            }
        }
        //山套餐
        setmealMapper.deletes(ids);

        //删s_dish
        setmealMapper.deleteDish(ids);
    }
    /**
     * 修改回显
     * */

    @Override
    public SetmealHuiXianVO huixian(Long id) {
        SetmealHuiXianVO setmealHuiXianVO= setmealMapper.huixian(id);
        return setmealHuiXianVO;
    }
    /**
     * 修改套餐
     * */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(SetmealXiuGaiDTO setmealXiuGaiDTO) {
        //套餐名唯一
        Setmeal setmeal1 = setmealMapper.seleteByName(setmealXiuGaiDTO.getName());
        if(setmeal1==null){

        }else {
            if(Objects.equals(setmealXiuGaiDTO.getId(),setmeal1.getId())){

            }else {
                throw new BaseException("用户名不能重复");
            }
        }


        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealXiuGaiDTO,setmeal);
        setmealMapper.update(setmeal);
        List<SetmealDish> setmealDishes = setmealXiuGaiDTO.getSetmealDishes();
        //修改另一张表
        setmealMapper.deleteSD(setmealXiuGaiDTO.getId());
        Long id = setmealXiuGaiDTO.getId();
        for(SetmealDish setmealDish:setmealDishes){
            setmealDish.setSetmealId(id);
        }


        setmealMapper.addSetmealDish(setmealDishes);





    }
    /**
     * 启用禁用
     * */
    @Override
    public void qinyong(Integer status, Long id) {

        Setmeal setmeal=new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setmealMapper.update(setmeal);
    }
    /**
     * 用户端根据id查询套餐列表
     * */

    @Override
   // @Cacheable(value = RedisConstant.KEY_PREFIX_SETMEAL,key = "#categoryId")

    public List<Setmeal> seleteByCategoryId(Long categoryId) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(StatusConstant.ENABLE);


        List<Setmeal>setmeals= setmealMapper.seleteByCategoryId(setmeal.getStatus(),categoryId);
        return setmeals;
    }
    /**
     * 用户端根据套餐ID 查询菜品列表
     * */
    @Override

    public List<DishItemVO> getDishItemById(Long id) {
        List<DishItemVO>list=setmealMapper.getDishBymealId(id);
        return list;
    }
    /**
     * 根据套餐ID拿到套餐
     * */

    @Override
    public Setmeal getById(Long setmealId) {
        Setmeal byId = setmealMapper.getById(setmealId);
        return byId;
    }
}
